from django.db import models

# Create your models here.


class Grade(models.Model):
    """
    等级表：
    字段：等级
    """
    grade = models.CharField(max_length=20)

    class Meta:
        db_table = 'grade'

    def __str__(self):
        return self.grade


class User(models.Model):
    """
    用户表：
    包含字段：用户名称，密码，状态，邮箱
    """
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=128)
    state = models.BooleanField(default=0)
    email = models.EmailField(max_length=50)

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.username


class UserInfo(models.Model):
    """
    创建用户信息表：
    字段：用户（外键1对1关系），真实姓名，手机号，身份证号，QQ号，家庭地址
    """
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    name = models.CharField(max_length=32)
    phone = models.CharField(max_length=22)
    shenfz = models.CharField(max_length=36)
    qq = models.CharField(max_length=24)
    address = models.CharField(max_length=100)

    class Meta:
        db_table = 'userinfo'
    def __str__(self):
        return self.name


class Author(models.Model):
    """
    作者表：
    包含字段：作者名称，用户（外键1对1关系），作者状态
    """
    author_name = models.CharField(max_length=32)
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    state = models.BooleanField(default=0)

    class Meta:
        db_table = 'author'

    def __str__(self):
        return self.author_name


class Cate(models.Model):
    """
    分类表：
    包含字段：分类
    """
    cate = models.CharField(max_length=20)

    class Meta:
        db_table = 'cate'
    def __str__(self):
        return self.cate


class Story(models.Model):
    """
    小说表
    包含字段：小说名，作者（外键），价格，图片，简介，阅读量，分类（外键）
    """
    name = models.CharField(max_length=64)
    author = models.ForeignKey(Author,on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=6,decimal_places=2)
    release_time = models.DateField(auto_now_add=True)
    img = models.ImageField(upload_to='img')
    introduce = models.CharField(max_length=500)
    reading = models.IntegerField(default=0)
    cate = models.ForeignKey(Cate,on_delete=models.CASCADE)

    class Meta:
        db_table = 'story'

    def __str__(self):
        return self.name


class Section(models.Model):
    """"
    章节表：
    包含字段：章节名，小说（外键），内容
    """
    name = models.CharField(max_length=24)
    story = models.ForeignKey(Story,on_delete=models.CASCADE)
    content = models.TextField()

    class Meta:
        db_table = 'section'

    def __str__(self):
        return self.name





